//
//  FileSystemController.m
//
//  Created by Basuke on 08/03/16.
//  Copyright 2008 Basuke's. All rights reserved.
//

#import "FileSystemController.h"
#import "BasicApp.h"
#import "BuilderHelper.h"
#import "DescriptorView.h"


@implementation FileSystemController

@synthesize manager;
@synthesize path;
@synthesize list;
@synthesize navigation;

- (id)init {
	
	return [self initWithPath:[[BasicApp app] rootDirPath]];
}

- (id)initWithPath:(NSString *)aPath {
	if (self = [super init]) {
		self.manager = [NSFileManager defaultManager];
		self.path = aPath;
		self.list = [self.manager contentsOfDirectoryAtPath:self.path error:nil];
		
		self.title = [self.path lastPathComponent];
	}
	
	return self;
}

- (void) loadView {
	UITableView* view = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease];
	view.backgroundColor = [UIColor whiteColor];
	view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
	view.autoresizesSubviews = YES;
	
	// Set up table view
	view.delegate = self;
	view.dataSource = self;
	
	[view reloadData];
	
	self.view = view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	return [list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withAvailableCell:(UITableViewCell *)availableCell {
	UISimpleTableViewCell *cell = (UISimpleTableViewCell *)availableCell;;
	
	if (!cell) {
		CGRect frame = CGRectMake(0, 0, 300, 44);
		cell = [[[UISimpleTableViewCell alloc] initWithFrame:frame] autorelease];
	}
	
	NSString *name = [self.list objectAtIndex:[indexPath row]];
	NSString *targetPath = [self.path stringByAppendingPathComponent:name];
	
	BOOL isDirectory;
	
	
	if ([self.manager fileExistsAtPath:targetPath isDirectory:&isDirectory] && isDirectory) {
		cell.image = [UIImage imageNamed:@"folder.png"];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	} else {
		NSString *iconFile = [NSString stringWithFormat:@"%@.png", [targetPath pathExtension]];
		UIImage *icon = [UIImage imageNamed:iconFile];
		
		cell.image = (icon ? icon : [UIImage imageNamed:@"file.png"]);
		cell.accessoryType = UITableViewCellAccessoryNone;
	}

	cell.text = name;
	return cell;
}

- (void)tableView:(UITableView *)tableView selectionDidChangeToIndexPath:(NSIndexPath *)indexPath fromIndexPath:(NSIndexPath *)oldIndexPath {
	NSString *name = [self.list objectAtIndex:[indexPath row]];
	NSString *targetPath = [self.path stringByAppendingPathComponent:name];
	BOOL isDirectory;
	
	if ([self.manager fileExistsAtPath:targetPath isDirectory:&isDirectory]) {
		if (isDirectory) {
			FileSystemController *sub = [[[FileSystemController alloc] initWithPath:targetPath] autorelease];
			sub.navigation = self.navigation;
			
			[self.navigation pushViewController:sub animated:YES];
		} else {
			FileItemController *item = [[[FileItemController alloc] initWithPath:targetPath] autorelease];
			item.navigation = self.navigation;
			
			[self.navigation pushViewController:item animated:YES];
		}
	}
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

@implementation FileItemController

@synthesize manager;
@synthesize path;
@synthesize navigation;

- (id)initWithPath:(NSString *)aPath {
	if (self = [super init]) {
		self.manager = [NSFileManager defaultManager];
		self.path = aPath;
		self.title = [self.path lastPathComponent];
	}
	
	return self;
}

- (void) loadView {
	DescriptorView *descView = [[[DescriptorView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
	
	descView.backgroundColor = [UIColor whiteColor];
	descView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
	descView.autoresizesSubviews = YES;
	
	[descView addString:[self.path lastPathComponent] withLabel:@"Name"];
	
	NSDictionary *attr = [self.manager attributesOfFileSystemForPath:self.path error:nil];
	
	NSString *ext = [self.path pathExtension];
	
	if ([ext compare:@"png"] == 0) {
		UIImage *image = [UIImage imageWithContentsOfFile:self.path];
		[descView addString:[NSString stringWithFormat:@"%0.1f x %0.1f", image.size.width, image.size.height] withLabel:@"Dimension"];
		
		UIImageView *view = [[[UIImageView alloc] initWithImage:image] autorelease];
		[descView addSubview:view withLabel:@"Preview"];
	}
	
	self.view = descView;
}

@end
